package com.summer.safe.base;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
import com.summer.safe.system.entity.User;
import com.summer.safe.utils.Global;
import javax.xml.bind.annotation.XmlTransient;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;

import com.summer.safe.utils.StringUtils;
import com.summer.safe.utils.UserUtils;

/**
 * Entity支持类
 *
 */

public abstract class BaseEntity<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 删除标记（0：正常；1：删除；）
     */
    public static final String DEL_FLAG_NORMAL = "0";
    public static final String DEL_FLAG_DELETE = "1";

    /**
     * 实体编号（唯一标识）
     */
    protected String id;
    /**
     * 创建者
     */
    protected User createBy;
    /**
     * 创建日期
     */
    protected Date createDate;
    /**
     * 更新者
     */
    protected User updateBy;
    /**
     * 更新日期
     */
    protected Date updateDate;
    /**
     * 删除标记（0：正常；1：删除；）
     */
    protected String delFlag;
    /**
     * 备注
     */
    protected String remarks;

    /**
     * 操作类型(view,edit,add)
     */
    protected String op;



    /**
     * 自定义SQL（SQL标识，SQL内容）
     */
    protected Map<String, String> sqlMap;

    /*
     * 是否为新数据
     */
    protected boolean isNewRecord = false;
    /**
     * sql排序字段
     */
    protected String sqlSort;
    /**
     * sql排序规则
     */
    protected String sortOrder;

    /**
     * 排序
     */
    protected Integer sort;

    public String getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    public String getSqlSort() {
        return sqlSort;
    }

    public void setSqlSort(String sqlSort) {
        this.sqlSort = sqlSort;
    }

    public String dbName;

    public BaseEntity() {
        dbName = Global.dbName;
        this.delFlag = DEL_FLAG_NORMAL;
    }

    public BaseEntity(String id) {
        this();
        this.id = id;
        this.delFlag = this.delFlag = DEL_FLAG_NORMAL;
        ;
    }

    /**
     * 是否是新记录（默认：false），调用setIsNewRecord()设置新记录，使用自定义ID。
     * 设置为true后强制执行插入语句，ID不会自动生成，需从手动传入。
     *
     * @return
     */
    public boolean getIsNewRecord() {
        return isNewRecord || StringUtils.isBlank(getId());
    }

    /**
     * 是否是新记录（默认：false），调用setIsNewRecord()设置新记录，使用自定义ID。
     * 设置为true后强制执行插入语句，ID不会自动生成，需从手动传入。
     */
    public void setIsNewRecord(boolean isNewRecord) {
        this.isNewRecord = isNewRecord;
    }

    /**
     * 插入之前执行方法
     */
    public void preInsert() {
        // 不限制ID为UUID，调用setIsNewRecord()使用自定义ID
        if (!this.isNewRecord){
            setId(StringUtils.getUuid());
        }
        User user = UserUtils.getUser();
        if (!StringUtils.isEmpty(user.getId())) {
            this.updateBy = user;
            this.createBy = user;
        }
        this.updateDate = new Date();
        this.createDate = this.updateDate;
    }




    /**
     * 更新之前执行方法
     */
    public void preUpdate() {
        User user = UserUtils.getUser();
        if (!StringUtils.isEmpty(user.getId())) {
            this.updateBy = user;
        }
        this.updateDate = new Date();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }



    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }


    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public String getDelFlag() {
        return delFlag;
    }

    public void setDelFlag(String delFlag) {
        this.delFlag = delFlag;
    }

    @Override
    public boolean equals(Object obj) {
        if (null == obj) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (!getClass().equals(obj.getClass())) {
            return false;
        }
        BaseEntity<?> that = (BaseEntity<?>) obj;
        return null == this.getId() ? false : this.getId().equals(that.getId());
    }

    public User getCreateBy() {
        return createBy;
    }

    public void setCreateBy(User createBy) {
        this.createBy = createBy;
    }

    public User getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(User updateBy) {
        this.updateBy = updateBy;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }


    @JsonIgnore
    @XmlTransient
    public Map<String, String> getSqlMap() {
        if (sqlMap == null) {
            sqlMap = Maps.newHashMap();
        }
        return sqlMap;
    }


    public void setSqlMap(Map<String, String> sqlMap) {
        this.sqlMap = sqlMap;
    }



    public String getOp() {
        return op;
    }

    public void setOp(String op) {
        this.op = op;
    }


}